<template>
  <div>
    <nav-bar-back>
      <img
        class="left"
        src="../../assets/img/navbar/ic_title_close.png"
        slot="left"
        alt=""
        @click="$router.back()"
      />
      <div slot="center" class="center">
        <span
          v-for="(title, id) in titles"
          :key="id"
          :class="[currentTitle === id ? 'title_active' : '']"
          @click="titleClick(id)"
        >
          {{ title }}</span
        >
      </div>
      <div slot="right" class="right">
        <img
          src="../../assets/img/navbar/ic_header_search.png"
          alt=""
          @click="serachClick"
        />
        <img
          src="../../assets/img/navbar/ic_header_preparation.png"
          alt=""
          @click="screenShowClick"
        />
      </div>
    </nav-bar-back>
    <div class="tab_control">
      <ul>
        <li
          v-for="(item, index) in title"
          :key="index"
          @click="changeClick(index)"
        >
          <van-button
            round
            class="control_item"
            :class="currentIndex === index ? 'control_item_active' : ''"
            >{{ item }}</van-button
          >
        </li>
      </ul>
    </div>
    <div class="content_box" ref="viewhome">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <game-list :list="list" @detail-click="detailClick" />
          <empty-tip v-show="list.length === 0" v-cloak/>
        </van-list>
      </van-pull-refresh>
      <back-top :scrollTop="scTop" @back-top="backTop"/>
    </div>
    <search :show="show" @on-search="onSearch" @close-search="closeSearch" />
    <screen
      :show="isShow"
      @show-click="showClick"
      @screen-click="screenClick"
      :clientArr="clientArr"
      :genreList="genreList"
    />
  </div>
</template>

<script>
import NavBarBack from "@/components/common/navbar/NavBarBack.vue";
import Search from "@/components/common/search/Search.vue";
import Screen from "@/components/common/screen/Screen.vue";
import { getGameList } from "../../network/home";
import GameList from "../../components/content/gamelList/GameList.vue";
import BackTop from '../../components/common/backTop/BackTop.vue';
import mixinBackTop from '../../mixins/common'
import EmptyTip from '../../components/common/emptyImg/EmptyTip.vue';

export default {
  name: 'AllGame',
  mixins: [mixinBackTop],
  components: {
    NavBarBack,
    Search,
    Screen,
    GameList,
    BackTop,
    EmptyTip,
  },
  data() {
    return {
      show: false, //搜索弹窗
      isShow: false, //筛选弹窗
      active: 2,
      currentIndex: 0,
      currentType: "1",
      currentTitle: 0,
      title: ["BT", "折扣", "H5"],
      titles: ["全部游戏", "禁推游戏"],
      isLoading: false,
      loading: true,
      finished: false,
      list: [],
      page: 1,
      clientArr: [],
      genreList: [],
      clientType: "",
      genreId: "",
      scrollTop: '',
    };
  },
  beforeRouteLeave (to, from, next) {
    this.scrollTop = document.documentElement.scrollTop || document.querySelector('.content_box').scrollTop
    console.log(this.scrollTop)
    next()
  },
  activated() {
    const top = document.querySelector('.content_box')
    top.scrollTo(0, this.scrollTop)
  },
  created() {
    this.getGameAll();
  },
  methods: {
    getGameAll() {
      this.finished = false;
      getGameList(
        this.currentType,
        this.page,
        this.clientType,
        this.genreId
      ).then((res) => {
        this.isLoading = false;
        if (res.state === "ok") {
          console.log(res.data);

          this.clientArr = res.data.client_arr;
          this.genreList = res.data.genre_list;
          if(res.data.list !== null) {
            this.list = [...this.list, ...res.data.list];
          }
          this.loading = false;
          if (res.data.list === null) {
            this.finished = true;
          }
        } else if (res.msg === "no_login") {
          this.$router.replace("/login");
        }
      });
    },
    onLoad() {
      this.page++
      this.getGameAll()
    },
    titleClick(id) {
      this.list = []
      this.page = 1
      this.finished = false
      this.currentTitle = id
      if (id === 0) {
        this.currentType = "1"
        this.getGameAll()
      } else if (id === 1) {
        this.currentType = "gh_forbid"
        this.getGameAll()
      }
    },
    changeClick(index) {
      this.list = [];
      this.page = 1;
      this.finished = false;
      this.currentIndex = index;
      if (this.currentTitle === 0) {
        if (index === 0) {
          this.currentType = "1"
        } else if (index === 1) {
          this.currentType = "2"
        } else if (index === 2) {
          this.currentType = "3"
        }
        this.getGameAll();
      }
    },
    screenClick(type, id) {
      this.isShow = false;
      this.clientType = type;
      this.genreId = id;
      this.list = [];
      this.page = 1;
      this.getGameAll();
    },
    closeSearch() {
      this.show = false;
    },
    showClick() {
      this.isShow = !this.isShow;
    },
    serachClick() {
      this.show = true;
    },
    screenShowClick() {
      this.isShow = true;
    },
    onSearch(val) {
      //发起网络请求
      console.log(val);
    },
    detailClick(id) {
      this.$router.push("/gameinfo/gameid/" + id);
    },
    //下拉刷新
    onRefresh() {
      this.list = [];
      this.page = 1;
      this.getGameAll();
    },
    showClick() {
      this.isShow = !this.isShow;
    },
  },
};
</script>

<style scoped>
.left {
  width: 44px;
  height: 44px;
}

.center {
  color: #666;
}

.center span {
  box-sizing: border-box;
  display: inline-block;
  height: 88px;
  margin: 0 20px;
  font-weight: 450;
}

.title_active {
  color: #333;
  border-bottom: 6px solid #344ede;
}

.right {
  display: flex;
  align-items: center;
}

.right img {
  width: 44px;
  height: 44px;
  margin-left: 20px;
}

.tab_control {
  box-sizing: border-box;
  padding: 16px 35px;
  width: 750px;
  height: 98px;
  background: #eee;
  position: fixed;
  top: 88px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
}

.tab_control ul {
  display: flex;
}

.control_item {
  width: 160px;
  height: 66px;
  font-size: 28px;
  margin-right: 20px;
  border: 0;
  color: #333;
  background: #fff;
}

.control_item_active {
  color: #fff;
  background: rgb(129, 129, 129);
}

.content_box {
  width: 750px;
  margin-top: 186px;
  margin-left: 50%;
  transform: translateX(-50%);
  background: #fff;
}
</style>